(function () {
  var oProvince = document.querySelector("#province");
  var oCity = document.querySelector("#city");
  var oSchool = document.querySelector("#school");

  function init() {
    // 给省会添加options
    addProvince();
    oProvince.addEventListener("change", function (e) {
      oCity.innerHTML = "";
      oSchool.innerHTML = "";
      var val = e.target.value;
      var selectCity = city[val];
      addCity(selectCity, oCity);
      var cityVal = oCity.value;
      addSchool(allschool[cityVal], oSchool);
    });
    oCity.addEventListener("change", function (e) {
      oSchool.innerHTML = "";
      var val = e.target.value;
      var selectSchool = allschool[val];
      addSchool(selectSchool, oSchool);
    });
  }
  // 添加省会 元素
  function addProvince() {
    var provinceArr = Object.entries(province);
    addOptions(provinceArr, oProvince);
  }

  function addCity(selectCity, father) {
    var cityArr = Object.entries(selectCity);
    addOptions(cityArr, father);
  }

  function addSchool(selectSchool, father) {
    for (var i = 0; i < selectSchool.length; i++) {
      var option = document.createElement("option");
      option.value = i;
      option.innerText = selectSchool[i];
      father.appendChild(option);
    }
  }

  function addOptions(arr, father) {
    for (var i = 0; i < arr.length; i++) {
      var option = document.createElement("option");
      option.value = arr[i][0];
      option.innerText = arr[i][1];
      father.appendChild(option);
    }
  }

  init();
})();
